package com.example.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.example.base.app.RouterPath
import com.tencent.mmkv.MMKV

/**
 * 页面路由Token拦截器
 */
@Interceptor(priority = 2, name = "路由Token拦截器")
class RouteTokenInterceptor:IInterceptor {
    private lateinit var mContent:Context
    override fun init(context: Context?) {
        context?.let {
            mContent=it
        }
    }

    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        //获取Token
        val loginToken=MMKV.defaultMMKV().decodeString("token","video")
        if (loginToken.isNullOrEmpty()&& !postcard?.path.equals(RouterPath.PATH_LOGIN_PAGE)){
            postcard?.group=RouterPath.GROUP_LOGIN
            postcard?.path= RouterPath.PATH_LOGIN_PAGE
            postcard?.navigation()
        }else{
            callback?.onContinue(postcard)
        }
    }
}